<?php

include('../DB.php');

// 添加爬虫任务到数据库队列

class Tasks2Queue
{
    private $options;
    private $queueDB;
    private $writeDB;
    private $readDB;
    private $info;

    public function __construct($argv)
    {
        $dbConfig = include('../dbconfig.php');
        $this->parseArgv($argv);

        $this->connDB($dbConfig);
    }

    public function parseArgv($argv)
    {
        $shortOpts = "v::h::l:";
        $longOpts = ["verbose::", "help::", "limit:"];
        $options = getopt($shortOpts, $longOpts);

        foreach ($options as $k => $v) {
            if ($k=='h' || $k=='help') {
                $this->help();
            }
            if ($k=='l' || $k=='limit') {
                $this->options['limit'] = $v;
            }
            if ($k== 'v' || $k== 'verbose') {
                $this->info = true;
            }
        }
    }

    public function connDB($dbConfig)
    {
        $config = $dbConfig['queue'];
        $this->queueDB = new DB($config['host'], $config['user'], $config['pass'], $config['dbname']);

        $config = $dbConfig['write'];
        $this->writeDB = new DB($config['host'], $config['user'], $config['pass'], $config['dbname']);

        $config = $dbConfig['read'];
        $this->readDB = new DB($config['host'], $config['user'], $config['pass'], $config['dbname']);
    }

    public function help()
    {
        echo 'help info...';
    }

    public function run()
    {
        // 查询数据库队列中的任务数
        $sql = "SELECT COUNT(*) AS 'pending' FROM queue";
        $rows = $this->queueDB->query2Array($sql, "", true);
        // 限制队列行数
        if ($rows['pending'] > $this->options['limit']) {
            throw new \Exception('has many task in queue');
        }

        // 从数据库表查询上次未抓取的或者上次抓取未成功的爬虫任务 todo
        $tasks = [
            ['value' =>  'http://www.baidu.com'],
            ['value'    =>  'http://www.sina.com'],
        ];
        $values = [];
        if ($tasks) {
            foreach ($tasks as $value) {
                $values[] = $value;
            }
        }

        // 添加到数据库队列
        if ($values) {
            $sql = "INSERT INTO queue (value) VALUES ".implode(",", $values);
            $this->writeDB->query($sql);
        }
    }
}

$client = new Tasks2Queue($argv);
$client->run();
